package view;

import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ActionEvent;

import model.*;

/**
 * @author Gerhard
 * @version 08-02-2012
 * Project: Vossen en Konijnen
 */

public class PieChartView extends AbstractView
{
	private static final long serialVersionUID = 9127684790096135815L;

	/**
	 * Abstractview gets the right size (see below)
	 */
	public PieChartView(Simulator simulator) 
	{
		super(simulator);		
	}
	
	/**
	 * Methode berekend de grote van de delen en stopt ze in een array]
	 * Method calculates actor array by their length and calculates the size in the piechart (a circle factor of 360)
	 */
	private int[] calculateParts() 
	{
		int[] actorCountArray = simulator.getActors();
		int[] parts;
			
		double total = 0;
		double factor = 0;
			
		for(int i = 1; i < actorCountArray.length; i++) 
		{
			total += actorCountArray[i];
		}
			
		factor = total / 360;
			
		parts = new int[actorCountArray.length - 1];
			
		for(int i = 1; i < actorCountArray.length; i++) 
			{
				double a = actorCountArray[i];
				double p = a / factor;
				int p2 = (int) p;
				
				parts[i-1] = p2;
			}
			return parts;
		}
	
	/**
	 * Method, gets statuses
	 */
	public void paintComponent(Graphics g) 
	{
		int[] parts = calculateParts();
		
		g.setColor(Color.white);
		g.fillRect(0, 0, 172, 153);
		
		g.setColor(Color.white);
		g.fillRect(10, 17, 140, 120);
		
		int nextPie = 0;
		
		//Draw elk onderdeel voor actor
		if(parts[0] > 0) 
		{
			g.setColor(simulator.getColor(Rabbit.class));
			g.fillArc(30, 30, 100, 100, nextPie, 360 -nextPie);
			nextPie += parts[0];
		}
		
		if(parts[1] > 0) 
		{
			g.setColor(simulator.getColor(Fox.class));
			g.fillArc(30, 30, 100, 100, nextPie, 360 -nextPie);
			nextPie += parts[1];
		}
		
		if(parts[2] > 0) 
		{
			g.setColor(simulator.getColor(Redfox.class));
			g.fillArc(30, 30, 100, 100, nextPie, 360 - nextPie);
			nextPie += parts[2];
		}
	}

	protected void createView() 
	{
		setSize(172,153);
		setVisible(true);
	}
}
